﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.Interfaces.BasicEntities;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.UI.ProjectExplorer;
using SoftwareConsulting.BI11.Studio.UI.Properties;
using SoftwareConsulting.BI11.Studio.InternalServices;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.SituationsHierarchy
{
    /// <summary>
    /// Диалог блокировки характеристик при удалении связей
    /// </summary>
    public partial class LinkDeletingDialog : RestorableForm
    {
        private INamedHierarchy<Situation> _childSituations; //ситуации с унаследованными характеристиками
        private Hashtable characteristics; //список характеристик
        private ProjectExplorerForm _projectExplorerLink; //связь с формой обозревателя проекта

        /// <summary>
        /// Подготовка списка унаследованных характеристик
        /// </summary>
        private void PrepareList()
        {
            characteristics = new Hashtable();
            CharacteristicsListBox.Items.Add(Resources.BI_SITUATIONSHIERARCHY_LINK_DELETING_ALL, false);
            foreach (Situation sit in _childSituations)
            {                
                foreach(Characteristic ch in sit)
                {                    
                    if (ch.ParentCharacteristicId != Guid.Empty)
                    {
                        characteristics.Add(ch.Id, ch);
                        if (!CharacteristicsListBox.Items.Contains(ch.Name))
                        {
                            CharacteristicsListBox.Items.Add(ch.Name, false);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Проверка состояния флага опроса на блокировку
        /// </summary>
        private void CheckPrompt()
        {
            if (NoPromptMoreCheckBox.Checked)
            {
                _projectExplorerLink.CurrentSettings.CharacteristicsBlockingAbility = false;                
            }
        }

        /// <summary>
        /// Блокировка характеристик и закрытие диалога
        /// </summary>
        private void BlockAndClose()
        {
            int c = CharacteristicsListBox.Items.Count;
            for (int i = 1; i < c; i++)
            {
                if (CharacteristicsListBox.GetItemChecked(i))
                {                                    
                    foreach(DictionaryEntry entry in characteristics)
                    {                        
                        Characteristic ch = entry.Value as Characteristic;
                        if (ch.Name == CharacteristicsListBox.Items[i].ToString())
                        {
                            ch.ParentCharacteristicId = Guid.Empty;
                            ch.BlockedWhileLinkDeleting = true;
                        }
                    }
                }
            }
            CheckPrompt();
        }

        /// <summary>
        /// Отказ от блокировки и закрытие диалога
        /// </summary>
        private void CancelAndClose()
        {
            CheckPrompt();
        }

        /// <summary>
        /// Обработка отметки флажка
        /// </summary>
        /// <param name="index"></param>
        /// <param name="checkState"></param>
        private void ProcessCheck(int index, CheckState checkState)
        {
            if (index == 0)
            {
                int c = CharacteristicsListBox.Items.Count;
                for (int i = 1; i < c; i++)
                {
                    CharacteristicsListBox.SetItemChecked(i, checkState==CheckState.Checked);
                }                
            }
        }

        /// <summary>
        /// Инициализация компонентов
        /// </summary>
        public LinkDeletingDialog(INamedHierarchy<Situation> newChildSituations, ProjectExplorerForm newProjectExplorerLink)
        {
            _projectExplorerLink = newProjectExplorerLink;
            _childSituations = newChildSituations;
            InitializeComponent();            
        }

        private void LinkDeletingDialog_Load(object sender, EventArgs e)
        {
            PrepareList();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            BlockAndClose();
        }

        private void CharacteristicsListBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            ProcessCheck(e.Index,e.NewValue);
        }

        private void CancelLDButton_Click(object sender, EventArgs e)
        {
            CancelAndClose();
        }

    }
}
